package com.magnificent.panda.model.generator.flow;

import java.util.ArrayList;
import java.util.Random;

/**
 * User: Babanin
 * Date: 21.09.2009
 * Time: 21:37:44
 */
public class AdditiveFlow extends AbstractFlow {
    private Random rand = new Random();
    private int len;
    private double intensity;//интенсивность
    private int modelTime;
    private double samplingInterval;
    private ArrayList<Double> exitX;
    private double[] innerVector;

    public AdditiveFlow(int intensity1, int intensity2, int modellingTime) {
        super(intensity1, intensity2, modellingTime);
        
        this.len = (int) intensity1 * modellingTime;
        this.intensity = modellingTime / intensity1;
        this.modelTime = modellingTime;
        this.samplingInterval = (double) modellingTime / this.len;
        System.out.println("intensity" + intensity);
    }

    public double getPeriod() {
        double period = 0;
        for (int i = 0; i < innerVector.length - 1; i++) {
            period = period + (innerVector[i + 1] - innerVector[i]);
        }
        period = period / (innerVector.length - 1);
        return period;
    }

    public double[] generate() {
        exitX = new ArrayList<Double>();
        exitX.add(Double.valueOf(0));
        double eps;
        while (exitX.get(exitX.size() - 1) <= modelTime) {
            eps = samplingInterval + intensity * rand.nextGaussian();
            //System.out.println(eps);
            exitX.add(Double.valueOf(eps + exitX.get(exitX.size() - 1)));
        }

        Double exitArray[] = new Double[exitX.size()];
        exitArray = exitX.toArray(exitArray);
        double exArray[] = new double[exitArray.length];
        for (int i = 0; i < exitArray.length; i++) {
            exArray[i] = exitArray[i];
        }
        innerVector = exArray;
        return exArray;
    }
}

  /* converted to utf8 */